<script setup lang="ts">
// import { onMounted, onUnmounted, ref } from 'vue'
import { getDoctorPage } from '@/services/consult'
import type { DoctorList } from '@/types/consult'
import { useWindowSize } from '@vueuse/core'
import { onMounted, onUnmounted, ref } from 'vue'
import DoctorCard from './DoctorCard.vue'
// 1.自己实现，组件初始化的时候计算滚动宽度，当页面尺寸改变需要重新计算
// 宽度，组件销毁时需要清除事件
/* const width = ref(150)
const setWidth = () => (width.value = (150 / 375) * window.innerWidth)
onMounted(() => {
  setWidth()
  // window.addEventListener('resize', () => {
  //  测试 // console.log(window.innerWidth)
  //   setWidth()
  // })
  // 简写：
  window.addEventListener('resize', setWidth)
})
onUnmounted(() => {
  window.removeEventListener('resize', setWidth)
}) */

// 2.通过vueuse/core实现
const { width } = useWindowSize()
// 3.定义,请求,渲染 数据
const List = ref<DoctorList>([])
const loadData = async () => {
  const res = await getDoctorPage({ current: 1, pageSize: 5 }) // 只拿第一页的医生
  List.value = res.data.rows
}
onMounted(() => loadData()) // 组件渲染完成,即调用 api请求
onUnmounted(() => loadData()) // 之后,删除
</script>

<template>
  <div class="follow-doctor">
    <div class="head">
      <p>推荐关注</p>
      <a href="javascript:;">
        查看更多 <i class="van-icon van-icon-arrow" />
      </a>
    </div>
    <div class="body">
      <!-- swipe 组件 -->
      <van-swipe
        :loop="false"
        :width="(150 / 375) * width"
        :show-indicators="false"
      >
        <van-swipe-item v-for="item in List" :key="item.id">
          <doctor-card :item="item" />
        </van-swipe-item>
      </van-swipe>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.follow-doctor {
  background-color: var(--cp-bg);
  height: 250px;
  .head {
    display: flex;
    justify-content: space-between;
    height: 45px;
    align-items: center;
    padding: 0 15px;
    font-size: 13px;
    > a {
      color: var(--cp-tip);
    }
  }
  .body {
    width: 100%;
    overflow: hidden;
  }
}
</style>
